package com.thinkgsp.webapp.controller.rp;

import java.io.IOException;
import java.io.OutputStream;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jfree.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.thinkgsp.jxc.model.Emp;
import com.thinkgsp.jxc.model.RpAPayBill;
import com.thinkgsp.jxc.model.RpARecBill;
import com.thinkgsp.jxc.model.RpArpBill;
import com.thinkgsp.jxc.model.RpOtherAPayBill;
import com.thinkgsp.jxc.service.RpArpBillService;
import com.thinkgsp.jxc.service.ServiceException;
import com.thinkgsp.sys.model.SysAccountPeriod;
import com.thinkgsp.sys.service.NumberRoleService;
import com.thinkgsp.webapp.controller.BaseController;
import com.thinkgsp.webapp.util.ExportExcel;
import com.thinkgsp.webapp.util.ExtReturn;

/**
 * 应付单控制器
 * @author evan
 *
 */
@Controller
@RequestMapping(value="/rp/apaybill")
public class APayBillController extends BaseController {

	@Autowired
	private RpArpBillService arpBillService;
	@Autowired
	private NumberRoleService numberRoleService;
	
	/**
	 * 查询应收单
	 * @return
	 */
	@RequestMapping(value="/query.do")
	@ResponseBody
	public Object queryARecBills(@RequestParam("begDate") Date begDate,
								@RequestParam("endDate")Date endDate,
								@RequestParam("custId")Integer custId,
								@RequestParam("start") int start,
								@RequestParam("limit") int limit) {
		if(custId == null || custId == 0) {
			return arpBillService.getAPayBillList(begDate, endDate,start,limit);
		} else {
			return arpBillService.getAPayBillList(begDate, endDate, custId,start,limit);
		}
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping(value="/createAPayBill.do",method=RequestMethod.GET)
	@ResponseBody
	public Object createAPayBill() {
		RpOtherAPayBill apayBill = new RpOtherAPayBill();
		SysAccountPeriod period = arpBillService.getCurrentPeriod();
		apayBill.setFyear(period.getFyear());
		apayBill.setPeriod(period.getPeriod());
		Emp emp = arpBillService.getCurrentEmp();
		apayBill.setEmp(emp);
		apayBill.setMaker(emp);
		apayBill.setDept(arpBillService.getCurrentUser().getLogonDept());
		try {
			String code = numberRoleService.newNumber(7);
			apayBill.setArpBillCode(code);
		} catch(ServiceException e) {
			Log.error("New OtherAPayBill codeNumber error:" + e.getMessage());
		}
		List<RpArpBill> bills = new ArrayList<RpArpBill>();
		bills.add(apayBill);
		return bills;
	}
	
	@RequestMapping(value="/getAPayBill.do")
	@ResponseBody
	public Object getAPayBill(@RequestParam Long billId) {
		try {
			RpAPayBill bill = arpBillService.getArpBill(billId);
			List<RpArpBill> bills = new ArrayList<RpArpBill>();
			bills.add(bill);
			return bills;
		} catch(Exception e) {
			return new ExtReturn("获取应付单据出错");
		}
	}
	
	@RequestMapping(value="/save.do")
	@ResponseBody
	public Object saveAPayBill(@RequestBody RpAPayBill bill) {
		if(bill == null) {
			return new ExtReturn("单据无效。");
		}
		try {
			arpBillService.saveArpBill(bill);
			return new ExtReturn(true,"单据保存成功!");
		} catch(Exception e) {
			return new ExtReturn("保存应付单据失败，原因:" + e.getMessage());
		}
	}
	
	@RequestMapping(value="/archive.do")
	@ResponseBody
	public Object archiveAPayBill(@RequestParam Long billId) {
		if(billId == null || billId == 0L) {
			return new ExtReturn("参数无效。");
		}
		try {
			arpBillService.archiveAPayBill(billId);
			return new ExtReturn(true,"应付单归档成功!");
		} catch(Exception e) {
			return new ExtReturn(false,"单据归档失败,原因:" + e.getMessage());
		}
	}
	
	@RequestMapping(value="/unArchive.do")
	@ResponseBody
	public Object unArchiveAPayBill(@RequestParam Long billId) {
		if(billId == null || billId == 0L) {
			return new ExtReturn("参数无效。");
		}
		try {
			arpBillService.unArchiveAPayBill(billId);
			return new ExtReturn(true,"应付单反归档成功!");
		} catch(Exception e) {
			return new ExtReturn(false,"单据反归档失败,原因:" + e.getMessage());
		}
	}
	
	@RequestMapping(value="/delete.do")
	@ResponseBody
	public Object deleteAPayBill(@RequestParam Long billId) {
		if(billId == null || billId == 0L) {
			return new ExtReturn("参数无效。");
		}
		try {
			arpBillService.deleteArpBill(billId);
			return new ExtReturn(true,"应收单删除成功!");
		} catch(Exception e) {
			return new ExtReturn(false,"单据删除失败,原因:" + e.getMessage());
		}
	}
	
	@RequestMapping("/getApaybillXLS.do")
    public void  getXLS(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		try{
			Date begDate=Date.valueOf(request.getParameter("begDate"));
			Date endDate=Date.valueOf(request.getParameter("endDate"));
			String cid=request.getParameter("custId");
			int custId=0;
			if(StringUtils.isNotEmpty(cid) && !"0".equals(cid)) {
				custId = Integer.parseInt(cid);
			} 
			
			response.setContentType("octets/stream");
	        response.addHeader("Content-Disposition","attachment;filename="+ new String(("应付单导出").getBytes("GB2312"),"iso8859-1")+".xls");
	        List<RpArpBill> cb=new ArrayList<RpArpBill>();
	        
	        if(custId == 0) {
				cb.addAll(arpBillService.getAPayBillList(begDate, endDate));
			} else {
				cb.addAll(arpBillService.getAPayBillList(begDate, endDate, custId));
			}
	        OutputStream out = response.getOutputStream();
	        ExportExcel<RpArpBill> ex = new ExportExcel<RpArpBill>();
	        HSSFWorkbook book= ex.exportExcelByFieldAnnotation(cb,true);
	        book.write(out);
	        
		}catch(Exception e){
			
		}		 
	}
}
